package cn.iocoder.yudao.module.promotion.service.shortlinkgeneratee;

import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.ShortLinkGenerateeCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.ShortLinkGenerateeExportReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.ShortLinkGenerateePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.ShortLinkGenerateeUpdateReqVO;
import cn.iocoder.yudao.module.promotion.convert.shortlinkgeneratee.ShortLinkGenerateeConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.domainmanage.DomainManageDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.shortlinkgeneratee.ShortLinkGenerateeDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.shortlinkgeneratee.ShortLinkGenerateeMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.SHORT_LINK_GENERATEE_NOT_EXISTS;

/**
 * 短链接生成 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ShortLinkGenerateeServiceImpl implements ShortLinkGenerateeService {

    @Resource
    private ShortLinkGenerateeMapper shortLinkGenerateeMapper;

    @Override
    public Long createShortLinkGeneratee(ShortLinkGenerateeCreateReqVO createReqVO) {
        // 插入
        ShortLinkGenerateeDO shortLinkGeneratee = ShortLinkGenerateeConvert.INSTANCE.convert(createReqVO);
        shortLinkGenerateeMapper.insert(shortLinkGeneratee);
        // 返回
        return shortLinkGeneratee.getId();
    }

    @Override
    public void updateShortLinkGeneratee(ShortLinkGenerateeUpdateReqVO updateReqVO) {
        // 校验存在
        validateShortLinkGenerateeExists(updateReqVO.getId());
        // 更新
        ShortLinkGenerateeDO updateObj = ShortLinkGenerateeConvert.INSTANCE.convert(updateReqVO);
        shortLinkGenerateeMapper.updateById(updateObj);
    }

    @Override
    public void deleteShortLinkGeneratee(Long id) {
        // 校验存在
        validateShortLinkGenerateeExists(id);
        // 删除
        shortLinkGenerateeMapper.deleteById(id);
    }

    private void validateShortLinkGenerateeExists(Long id) {
        if (shortLinkGenerateeMapper.selectById(id) == null) {
            throw exception(SHORT_LINK_GENERATEE_NOT_EXISTS);
        }
    }

    @Override
    public ShortLinkGenerateeDO getShortLinkGeneratee(Long id) {
        return shortLinkGenerateeMapper.selectById(id);
    }

    @Override
    public List<ShortLinkGenerateeDO> getShortLinkGenerateeList(Collection<Long> ids) {
        return shortLinkGenerateeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShortLinkGenerateeDO> getShortLinkGenerateePage(ShortLinkGenerateePageReqVO pageReqVO) {
        return shortLinkGenerateeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShortLinkGenerateeDO> getShortLinkGeneratee(ShortLinkGenerateePageReqVO pageReqVO) {
        return shortLinkGenerateeMapper.selectList(pageReqVO);
    }

    @Override
    public List<ShortLinkGenerateeDO> getShortLinkGenerateeList(ShortLinkGenerateeExportReqVO exportReqVO) {
        return shortLinkGenerateeMapper.selectList(exportReqVO);
    }

    @Override
    public void closeShortLinkGeneratee(Long id) {
        // 校验存在
        validateShortLinkGenerateeExists(id);
        // 更新
        ShortLinkGenerateeDO updateObj = new ShortLinkGenerateeDO().setId(id).setStatus((byte) 1);
        shortLinkGenerateeMapper.updateById(updateObj);
    }
}
